In [4]:
list1=[1,2,3,4]
print(list1)
list2=["a","b","c","d"]
print(list2)
list3=[True, False, True]
print(list3)
list3=[1,2,"c","d"]
print(list3)
list4=[2>3, 2, 4, 3>2]
print(list4)
Let's notice a few key things:
In [9]:
la=[1,2,3]
lb=["1","2","3"]
print(la+lb)
la.append(4)
print(la)
lb+=la
print(lb)
At one point, you will want to access certain elements of a list, this is done by slicing. There are a couple of ways to do this.
In [11]:
la=[1,2,3,4,5,6,7,8,9,10]
print(la[0])
print(la[3])
print(la[-1])
print(la[-3])
print(la[0:10:2])
print(la[3::2])
print(la[2:4:])
print(la[2:4])
Make 2 lists and add them together.
Take your list from 1., append the list ["I", "Love", "Python"] to it without using the append command, then print every second element.
Can you make a list of lists? Try it.
Consider the list x=[3,5,7,8,"Pi"].
4a. Type out what you would expect python to print along with the type of object printed would be for the following slices:
x[2]
x[0]
x[-2]
x[1::2]
x[1::]
x[::-4]
4b. Check your answer by creating that list and printing out the corresponding slices.
Sets are a special type of list that adhere to certain rules. If you have taken any higher level or proof-based math classes, you will recognize that sets in Python are exactly the same as those in mathematics. Instead of using [], {} are used to create a set. Sets have the following properties:
In [17]:
t = {1,2,3,3,3,3,3,3,3,3,3,3,3,3,3,4,5}
print(t)
s = {1,4,5,7,8}
print(t-s)
print(s-t)
print(t^s)
print(t-s|s-t)
print(t^s==t-s|s-t)
In [18]:
a = (1,2,3,4,5)
b = ('a', 'b', 'c')
print(a)
print(b)
print(a+b)
Dictionaries are quite different from any container we have seen so far. A dictionary is a bunch of unordered key/value pairs. That is, each element of a dictionary has a key and a value and the elements are in no particular order. It is good to keep this unorderedness in mind later on, for now, let's look at some examples. To create a dictionary we use the following syntax, { key:value}.
In [57]:
#Let's say we have some students take a test and we want to store their scores
scores = {'Sally':89, 'Lucy':75, 'Jeff':45, 'Jose':96}
print(scores)
#We can, however, not combine two different dictionaries
scores2 = {'Devin':64, 'John':23, 'Jake':75}
print(scores2)
print(scores+scores2)
Unlike with lists, we cannot access elements of the dictionary with slicing. We must instead use the keys. Let's see how to do this.
In [58]:
print(scores['Sally'])
print(scores2['John'])
As we can see, the key returns us the value. This can be useful if you have a bunch of items that need to be paired together.
In [68]:
print(scores.keys())
print(scores.values())
In [62]:
print('Devin' in scores2)
print(2 in a)
print('Hello World' not in scores)
But what if I want my set to be a list or my tuple to be a set? To convert between types of containers, you can use any of the following functions:
list() : Converts any container type into a list, for dictionaries it will be the list of keys.
tuple() : Converts any container type into a tuple, for dictionaries it will be the tuple of keys.
set() : Converts any container type into a set, for dictionaries it will be the set of keys. Note that as above, this will remove all duplicates.
In [77]:
a = [1,2,3]
b = (1,2,3)
c = {1,2,3}
d = {1:2,3:4}
print(list(b))
print(list(c))
print(list(d))
print(tuple(a))
print(tuple(c))
print(tuple(d))
print(set(a))
print(set(b))
print(set(d))
In [ ]: